/***********************************************************************
�ļ����ƣ�main(void)
��    �ܣ�������SIC1000_1014
��дʱ�䣺2024.11.5
�� д �ˣ�zhangbin
ע    �⣺
***********************************************************************/
#include "SEGGER_RTT.h"
#include "delay.h"
#include "serPort3.h"
#include "sys.h"
#include <float.h>
#include <stdio.h>
#include <string.h>
/* Includes ------------------------------------------------------------------*/
#include "DB0.h"
#include "bitLogic.h"
#include "convert.h"
#include "delay.h"
#include "global.h"
#include "main.h"
#include "stm32f4xx_hal_def.h"
#include "stm32f4xx_hal_gpio.h"
#include "stm32f4xx_hal_tim.h"
#include "sysconf.h"
#include "timer_operation.h"
#include "usertempconf.h"
#include "util.h"
#include <string.h>
#include "tech_hsc.h"
#include "extraPwm.h"
#include "util.h"

/********************Header_Include_Tag_Start*********************/
#include "OBCollects.h"
#include "DBCollects.h"
#include "DefaultVariableTable.h"
/********************Header_Include_Tag_End***********************/
volatile int  _Cnt;
volatile Time system_run_time = 0;

// ���� test_run.c �еĺ���
extern void RunAllTests(void);
u8          BootReset               = 0;   // CPU������λ
u16         GWs[10];                       // s��ʱ��ר�ã���ȷ��ʱ




/*-----------------Ӳ����̬ ------------*/
// Configuration_User configuration_User[] = {
//     // Index 	T  Start_AddressDI/AI Start_AddressDO
//     {1, DI8, 2, 0},       // I[2][0]
//     {2, DI16, 3, 0},      // I[3][0]
//     {3, DO8, 0, 2},       // Q_W[2][0]
//     {4, DO16, 0, 3},      // Q_W[3][0]
//     {5, DIO16, 5, 5},     // I[5][0]  Q_W[5][0]
//     {6, AI4_RTD, 2, 0},   // AI[2]-AI[5]
//     {7, AI4_13BIT, 6, 0}, // AI[6]-AI[9]
//     {8, AI8_13BIT, 10, 0} // AI[10]-AI[17]
// };
Configuration_User configuration_User[] = {
    // Index 	T  Start_AddressDI/AI Start_AddressDO
    {1, DIO16, 2, 2},                       // I[2][0] Q[2][0]
    {2, DIO16, 4, 4},                       // I[4][0] Q[4][0]
    {3, AI4_RTD, GET_START_ADDR(144), 0},   // AI[8]-AI[11]
    {4, AI4_13BIT, GET_START_ADDR(128), 0}, // AI[0]-AI[3]
};
void UpdateEnableStatus(void) {
  configuration_User[0].Enable = 1;
  configuration_User[1].Enable = 1;
  configuration_User[2].Enable = 1;
  configuration_User[3].Enable = 1;
}

/**
 * @brief 在此函数中开启高速脉冲计数器
 *
 */
void turn_on_hsc(void) {
  // StartHSCCapture(HSC_1, 1, 0);    // 启动1号高速脉冲计数器;计数方向++;初始值为：0;
  // StartHSCCapture(HSC_2, 1, 5000); // 启动2号高速脉冲计数器;计数方向++;初始值为：5000;
}
void turn_on_pwd(void) {
  // PWM_SetByFreqDuty(PWM_CH_1, 500, 50.0f, 100);
  // PTO_ON(PWM_CH_2);
}
/*-----------------������ -------------*/
uint32_t count = 0;
int      main(void) {
  /****************DefaultVariableTable_Init_Tag_Start*******************/
  DefaultVariableTable_Init();
  /****************DefaultVariableTable_Init_Tag_End*******************/

  /**************************DB_Init_Tag_Start***************************/
  // NOTE 放在上电读取之前
  init_DB1();
  init_DB20();
  init_DB22();
  init_DB51();
  init_DB52();
  init_DB60();
  init_DB70();
  init_DB80();
  init_DB81();
  init_DB91();
  init_DB90();
  init_DB101();
  init_DB110();
  init_DB132();
  init_DB120();

  /**************************DB_Init_Tag_End***************************/
  turn_on_pwd();
  turn_on_hsc();
  InitializationSys(); // �������� ��
  ser3_Init(19200, 8, 1, 0);
  SEGGER_RTT_ConfigUpBuffer(0, NULL, NULL, 512, SEGGER_RTT_MODE_NO_BLOCK_SKIP);
  ser3_Write("A", 5);
  // StepperMotor_Start(0, 5000, 7000, 0.1f, 0.1f);
  SEGGER_RTT_printf(0, "printf Test: %%c,         'S' : %c.\r\n", 'S');

  //	g_DB1_t->chuShiHuaAnJian.shuRuPeiZhiChuShiHua                = 1;
  //  g_DB1_t->chuShiHuaAnJian.shuChuPeiZhiChuShiHua               = 1;
  //  g_DB1_t->chuShiHuaAnJian.quanXiTongChuShiHua                 = 1;
  //  g_DB1_t->sheBeiXinXiJiPeiZhi.sheBeiXingHao                   = 3;
  //  g_DB1_t->sheBeiXinXiJiPeiZhi.tuZhiBanBen                     = 1;
  //  *DefaultVariableTable_t.tuZhiBanBenBiaoZhi                   = 1;
  //
  //	g_DB1_t->sheBeiXinXiJiPeiZhi.sheBeiPeiZhi.shuRuShuaXinPeiZhi = 1;
  //  g_DB1_t->sheBeiXinXiJiPeiZhi.sheBeiPeiZhi.chaoShengBoPeiZhi  = 1;
  //  g_DB1_t->shouDongAnNiu                                       = 1;

  /**************************OB_EXEC_Tag_Start***************************/
  OB1_Execute();

  /**************************OB_EXEC_Tag_End***************************/
  // RunAllTests();
  // StepperMotor_Start(0, 5000, 5000, 0, 0);
  // delay_ms(2);

  //  CTRL_PWM_S pwm_t = {PWM_CH_1, True, False, 0};
  //  CTRL_PTO_S pto_t = {
  //           .req       = 1,
  //           .pto       = PWM_CH_2,
  //           .frequency = 2000,
  //           .status    = PTO_STATUS_NOERROR,
  //  };
  //  CTRL_PWM(1, &pwm_t);
  //  CTRL_PTO(1, &pto_t, 5000);
  *DefaultVariableTable_t.FirstScan = True; // fristscan
  while(1) {
    /****************OB_Loop_EXEC_Tag_Start*******************/
    OB0_Execute();
    *DefaultVariableTable_t.FirstScan = False;
    /****************OB_Loop_EXEC_Tag_End*******************/
    // count=Get_PulseCount(0);
    TaskSys(); // ϵͳ������
    updataHSC();
    /*****************************************************GN0-7ʱ�ӿ���*********************************************/
    /****ʱ�����ü���ʾ*****/
  }
}
